from services.new_services.schemas.cviya_config import Block, BLOCKS
from services.new_services.schemas.response_to_front import ErrorMessages
from services.new_services.new_server_integration.schemas.block import Block
from services.new_services.new_server_integration.schemas.list_statuses import ListStatuses
from services.new_services.new_server_integration.schemas.common_configuration import BlockType
from services.new_services.new_server_integration.schemas.channels_configuration import BlockMode
from services.new_services.new_server_integration.schemas.response_message import ResponseMessage


def _get_config_by_cviya(needed_block_cviya: str) -> Block | ResponseMessage:
    block: Block
    for block in BLOCKS:
        if block.cviya == needed_block_cviya:
            return block
    else:
        return ResponseMessage(status=False, message="Выбранный тип блока не соответствует установленному параметру ЦВИЯ")


def _get_statuses_list_by_cviya(service_channel_3_enabled: bool, service_channel_4_enabled: bool, block_type: str,
                                block_cviya: str, block_mode: str) -> ListStatuses | ResponseMessage:

    # НОВЫЕ СТАТУСЫ ЭТО ТЕ КОТОРЫЕ ДОЛЖНЫ БЫТЬ ПО ЦВИЕ И РЕЖИМУ БЛОКА

    sms_statuses: list = []
    earfcn_statuses: list = []
    arfcn_statuses: list = []

    # В ЗАВИСИМОСТИ ОТ РЕЖИМА БЛОКА БЛОКИРУЕМ КАНАЛЫ
    # ВОЗВРАЩАЕМ ИХ ЗНАЧЕНИЯ МАССИВОМ BOOL
    third_channel_status: bool = service_channel_3_enabled
    fourth_channel_status: bool = service_channel_4_enabled

    arfcn_earfcn_status: bool | None = None if fourth_channel_status is False else fourth_channel_status

    fsr_third_channel_status: bool | None = third_channel_status
    fsr_fourth_channel_status: bool | None = fourth_channel_status

    block_config: Block | ResponseMessage = _get_config_by_cviya(block_cviya)
    if isinstance(block_config, ResponseMessage):
        return block_config

    if block_type == BlockType.FSR:
        if block_mode == BlockMode.WB:
            sms_statuses = [False, False, False, False]
            earfcn_statuses = [None, None]
            arfcn_statuses = [None, None]
        elif block_mode == BlockMode.WB_2G:
            sms_statuses = [False, False, False, False]
            earfcn_statuses = [fsr_third_channel_status, fsr_fourth_channel_status]
            arfcn_statuses = [fsr_third_channel_status, fsr_fourth_channel_status]
        elif block_mode == BlockMode.G2:
            sms_statuses = [False, False, False, False]
            earfcn_statuses = [None, None]
            arfcn_statuses = [fsr_third_channel_status, fsr_fourth_channel_status]
    elif block_type == BlockType.FSC:
        if block_config.sms_count == 4:
            if block_mode == BlockMode.WB:
                sms_statuses = [False, False, False, False]
                earfcn_statuses = [None, None]
                arfcn_statuses = [None, None]
            elif block_mode == BlockMode.WB_2G:
                sms_statuses = [False, None, None, False]
                earfcn_statuses = [arfcn_earfcn_status, arfcn_earfcn_status]
                arfcn_statuses = [arfcn_earfcn_status, arfcn_earfcn_status]
            elif block_mode == BlockMode.G2:
                sms_statuses = [False, False, None, False]
                earfcn_statuses = [None, None]
                arfcn_statuses = [arfcn_earfcn_status, arfcn_earfcn_status]
        elif block_config.sms_count == 2:
            if block_mode == BlockMode.WB:
                sms_statuses = [False, None, None, False]
                earfcn_statuses = [None, None]
                arfcn_statuses = [None, None]
            elif block_mode == BlockMode.WB_2G:
                sms_statuses = [False, None, None, False]
                earfcn_statuses = [arfcn_earfcn_status, arfcn_earfcn_status]
                arfcn_statuses = [arfcn_earfcn_status, arfcn_earfcn_status]
            elif block_mode == BlockMode.G2:
                sms_statuses = [False, None, None, False]
                earfcn_statuses = [None, None]
                arfcn_statuses = [arfcn_earfcn_status, arfcn_earfcn_status]
        else:
            if block_mode == BlockMode.WB:
                sms_statuses = [False, False, None, False]
                earfcn_statuses = [None, None]
                arfcn_statuses = [arfcn_earfcn_status, arfcn_earfcn_status]
            elif block_mode == BlockMode.WB_2G:
                sms_statuses = [False, None, None, False]
                earfcn_statuses = [arfcn_earfcn_status, arfcn_earfcn_status]
                arfcn_statuses = [arfcn_earfcn_status, arfcn_earfcn_status]
            elif block_mode == BlockMode.G2:
                sms_statuses = [False, False, None, False]
                earfcn_statuses = [None, None]
                arfcn_statuses = [arfcn_earfcn_status, arfcn_earfcn_status]

    if sms_statuses and earfcn_statuses and arfcn_statuses:
        return ListStatuses(sms_statuses_list=sms_statuses,
                            earfcn_statuses_list=earfcn_statuses,
                            arfcn_statuses_list=arfcn_statuses)
    else:
        return ResponseMessage(status=False, message=ErrorMessages.CVIYA_ERROR)
